package com.canbot.u05.activity.eat;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.guide.MapMsgBean;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.socket.StringMsgBean;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

/**
 * Created by zmp on 2017/5/17.
 * * 充电地图复位界面
 */

public class EatMapRestoreActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private ToastTools instance;

        private View viewById;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.guide_restoration_activity);
                EventBus.getDefault().register(this);
                TextView tv_title = (TextView) findViewById(R.id.guide_base_title_tv);
                tv_title.setText(R.string.guide_restore);
                instance = ToastTools.newInstance(this);
                viewById = findViewById(R.id.guide_restoration_fuwei_iv);
                viewById.setOnClickListener(this);
        }

        @Override
        protected void onDestroy() {
                EventBus.getDefault().unregister(this);
                super.onDestroy();
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMapEvent(StringMsgBean bean) {
                if (bean.getMsgType() == MsgType.MOBILE_PAD_MAP_TASK) {
                        String msgData = bean.getMsgData();
                        if (!TextUtils.isEmpty(msgData)) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                MapMsgBean mapMsgBean = new Gson().fromJson(msgData, MapMsgBean.class);
                                if (MapMsgBean.MOBILE_RESTORE_MAP == mapMsgBean.getMsgType()) {
                                        if (mapMsgBean.isSuccess()) {
                                                startActivity(new Intent(EatMapRestoreActivity.this,EatActivity.class));
                                                finish();
                                        }
                                        else {
                                                instance.showText("地图复位失败，请重试！");
                                        }
                                }
                        }
                }
        }
        private Dialog dialogLoading;
        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_restoration_fuwei_iv:
                                instance.showText("机器人复位");
                                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                                restoreMapInfo();
                                break;
                }
        }

        private void restoreMapInfo() {
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 7 + "");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                instance.showText(getString(R.string.network_error));
                        }
                });
        }

}
